 import request from '@/utils/requestutil'
 import {syncRequest} from '@/utils/httpRequet'
 class dicStore{
    constructor(){
        this.dic={};
    }
     getName(key,codes){
      if(codes==""||codes==null||codes==undefined){
        return "";
      }
        if(this.dic[key]==undefined){
             this.loadDic(key);
        }
        let t = [];
        if(codes.indexOf(",")==-1){
            t.push(codes);
        }else{
          t = codes.split(",");
        }
            let res=[];
            for(let s=0;s<t.length;s++){
                if(this.dic[key] && this.dic[key].info[t[s]]){
                    res.push(this.dic[key].info[t[s]])
                }else{
                    res.push(t[s]);
                }
            }
            return res.join(",");
    }
     loadDic(key){
       let {data}= syncRequest({
            url: 'dicload',
            urlparam: [key]
            });
        if(data){
           // .then(await function(dt){
            let temp={};
            for(let i=0;i<data.length;i++){
                temp[data[i].key]=data[i].value;
            }
            this.dic[key]={store:data,info:temp}
        }
           // });
    }
    getDicData(key,node){
        if(this.dic[key]==undefined){
            this.loadDic(key);
        }
        return this.dic[key].store;
    }
    //获取该字典所有数据，仅用于查看
   getSpecialDicName(key,code){
     if(this.dic[key]==undefined){
       this.loadDic(key);
     }
     let obj = this.dic[key].info;
     let val = obj[code];
     if(val == "" || val == null){
       return "";
     }
     return val;
   }
};
const dicManager=new dicStore();

export default dicManager
